/**
 *
 */
package org.suren.littlebird.ssh;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
 * @author suren
 *
 */
public class ScpUtils
{
	private String sshHost;
	private int sshPort;
	private String sshUser;
	private String sshPasswd;

	public ScpUtils(String sshHost, int sshPort, String sshUser, String sshPasswd)
	{
		this.sshHost = sshHost;
		this.sshPort = sshPort;
		this.sshUser = sshUser;
		this.sshPasswd = sshPasswd;
	}

	public boolean scp(String srcFile, String dstFile)
	{
		Session session = JSchSessionFactory.getSession(sshHost, sshPort, sshUser, sshPasswd);
		if(session != null)
		{
			ChannelSftp ftp = null;
			try
			{
				ftp = (ChannelSftp) session.openChannel(JSchSessionFactory.CHANNEL_SFTP);
				ftp.connect();

				ftp.put(srcFile, dstFile, new TestMonitor());

				return true;
			}
			catch (JSchException e)
			{
				e.printStackTrace();
			}
			catch (SftpException e)
			{
				e.printStackTrace();
			}
			finally
			{
				if(ftp != null)
				{
					ftp.disconnect();
				}
			}
		}

		return false;
	}
}
